<?php

namespace app\admin\controller\auth;

use app\admin\controller\Base;
use think\Request;

class Group extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $groupLes = db('auth_group')->select();
        $this->assign([
            'groupLes' => $groupLes,
        ]);
        return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if (!request()->isPost()) {
            $this->error('非法请求！');
        }
        $data = $request->post();
        $res = db('auth_group')->insert($data);
        if ($res) {
            $this->success('添加成功', url('index'));
        } else {
            $this->error('添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $authGroups = db('authGroup')->find($id);
        $this->assign([
            'authGroups' => $authGroups,
        ]);
        return view();
    }

    /**
     * 保存更新的资源
     */
    public function update()
    {
        if (!request()->isPost()) {
            $this->error('非法请求！');
        }

        $data = input('post.');
        if ($data['id'] == 1 && $data['status'] == 0) {
            $this->error('超级管理员不可禁用');
        }
        $save = db('authGroup')->update($data);
        if ($save !== false) {
            $this->success('修改成功！', 'index');
        } else {
            $this->error('修改失败！');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if ($id == 1) {
            $this->error('超级管理员不可删除');
        }
        $res = db('authGroup')->delete($id);
        if ($res == 1) {
            $this->success('删除成功', url('index'));
        } else {
            $this->error('删除失败');
        }
    }

    public function assignRule($id)
    {
        if (request()->isPost()) {
            $data = input('post.');
            $rules = implode(',', $data['rules']);
            $save = db('authGroup')->where(array('id' => $data['id']))->update(['rules' => $rules]);
            if ($save !== false) {
                $this->success('分配权限成功！');
            } else {
                $this->error('分配权限失败！');
            }
        }
        $data = db('authRule')->where(['pid' => 0])->select();
        foreach ($data as $k => $v) {
            $data[$k]['children'] = db('authRule')->where(['pid' => $v['id']])->select();
            foreach ($data[$k]['children'] as $k1 => $v1) {
                $data[$k]['children'][$k1]['children'] = db('authRule')->where(['pid' => $v1['id']])->select();
            }
        }
        //$id=input('id');
        $authGroups = db('authGroup')->find($id);
        $rules = explode(',', $authGroups['rules']);
        $this->assign([
            'authGroups' => $authGroups,
            'data' => $data,
            'rules' => $rules,
        ]);
        return view();
    }

    public function changeStatus($id, $status)
    {
        if ($id == 1) {
            $this->error('超级管理员不可禁用');
        }
        $res = db('authGroup')->update(['status' => $status, 'id' => $id]);
        if ($res == 1) {
            $this->success('状态修改成功', url('index'));
        } else {
            $this->error('状态修改失败');
        }
    }
}
